package com.pubinfo.passbook.common.model.ruler;

import cn.hutool.core.date.DateUtil;
import lombok.Data;
import org.dom4j.Element;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

@Data
public class PayInformation {
    private String id;
    private String eInvoiceId;
    //可报销金额--计算后结果保存到此字段
    private BigDecimal reimbursableSum = BigDecimal.ZERO;
    /*票据id前6位为票据类型*/
    private String eInvoiceCode;
    /*报销类型，0：门诊，1住院*/
    private Byte reimbursementType;
    /*金额合计*/
    private BigDecimal totalAmount = BigDecimal.ZERO;
    /*开票方代码*/
    private String invoicingPartyCode;
    /*开票方名称*/
    private String invoicingPartyName;
    /*统筹基金内容，包括统筹基金支付和本次起付标准*/
    private String fundPayAmountInfo;
    /*个人账户支付金额*/
    private BigDecimal accountPayAmount = BigDecimal.ZERO;
    /*个人现金支付金额*/
    private BigDecimal ownPayAmount = BigDecimal.ZERO;
    /*统筹基金支付*/
    private BigDecimal fundPayAmount = BigDecimal.ZERO;
    /*个人自付*/
    private BigDecimal selfpaymentAmount = BigDecimal.ZERO;
    /*个人自费*/
    private BigDecimal selfpaymentCost = BigDecimal.ZERO;
  /*个人自理*/
  private BigDecimal selfpaymentCare = BigDecimal.ZERO;
  /*项目详细清单,横版使用*/
  private List<Details> detailsList;
  /*票面明细信息,竖版使用*/
  private List<Detail> detailList;
  /*医院类型*/
  private String orgType;
  /*付款方身份证*/
  private String payerPartyCode;
  /*医保类型*/
  private String patientMedicalInsuranceType;
  private String medicalInsuranceType;
  //开票日期
  private Date issueDate;
  /*付款方姓名*/
  private String payerPartyName;
  /*本年支付*/
  private BigDecimal individualAccountCurrPayment = BigDecimal.ZERO;
  /*本年余额*/
  private BigDecimal individualAccountCurrBalance = BigDecimal.ZERO;
  /*历年支付*/
  private BigDecimal individualAccountPrevPayment = BigDecimal.ZERO;
  /*历年余额*/
  private BigDecimal individualAccountPrevBalance = BigDecimal.ZERO;
    
    /*统筹基金支付和本次起付标准*/
    private BigDecimal poolingFundPayment = BigDecimal.ZERO;
    private BigDecimal startPay = BigDecimal.ZERO;
    
    public PayInformation() {
    }
    
    public PayInformation(Element elm) {
    
        //横版票据解析
    
        Element elementMain = elm.element("EInvoiceData").element("Main");
        this.eInvoiceCode = elementMain.element("EInvoiceCode").getText();
        this.reimbursementType = eInvoiceCode.startsWith("330601") ? (byte) 0 : 1;
        this.totalAmount = new BigDecimal(elementMain.element("TotalAmount").getText());
        this.invoicingPartyCode = elementMain.element("InvoicingParty").element("InvoicingPartyCode").getText();
      this.invoicingPartyName = elementMain.element("InvoicingParty").element("InvoicingPartyName").getText();
      issueDate = DateUtil.parse(elementMain.element("IssueDate").getText(), "yyyyMMdd");
        Element mainExt = elementMain.element("MainExt");
        if (mainExt != null) {
            if (mainExt.element("FundPayAmount") != null) {
                this.fundPayAmount = new BigDecimal(mainExt.element("FundPayAmount").getText());
            } else {
                this.fundPayAmount = new BigDecimal(-0.0000999);
            }
            if (mainExt.element("FundPayAmountInfo") != null) {
                this.fundPayAmountInfo = mainExt.element("FundPayAmountInfo").getText();
            }
            if (mainExt.element("AccountPayAmount") != null) {
                this.accountPayAmount =  new BigDecimal(mainExt.element("AccountPayAmount").getText());
            }else {
                this.accountPayAmount = new BigDecimal(-0.0000999);
            }
          if (mainExt.element("OwnPayAmount") != null) {
            this.ownPayAmount = new BigDecimal(mainExt.element("OwnPayAmount").getText());
          } else {
            this.ownPayAmount = new BigDecimal(-0.0000999);
          }
          if (mainExt.element("SelfpaymentAmount") != null) {
            this.selfpaymentAmount = new BigDecimal(mainExt.element("SelfpaymentAmount").getText());
          } else {
            this.selfpaymentAmount = new BigDecimal(-0.0000999);
          }
          if (mainExt.element("MedicalInsuranceType") != null) {
            this.medicalInsuranceType = mainExt.element("MedicalInsuranceType").getText();
          }
          if (mainExt.element("SelfpaymentCost") != null) {
            this.selfpaymentCost = new BigDecimal(mainExt.element("SelfpaymentCost").getText());
          } else {
            this.selfpaymentCost = new BigDecimal(-0.0000999);
          }
          if (mainExt.element("SelfpaymentCare") != null) {
            this.selfpaymentCare = new BigDecimal(mainExt.element("SelfpaymentCare").getText());
          } else {
            this.selfpaymentCare = new BigDecimal(-0.0000999);
          }
            if (mainExt.element("IndividualAccountCurrPayment") != null) {
                this.individualAccountCurrPayment = new BigDecimal(mainExt.element("IndividualAccountCurrPayment").getText());
            } else {
                this.individualAccountCurrPayment = new BigDecimal(-0.0000999);
            }
            if (mainExt.element("IndividualAccountCurrBalance") != null) {
                this.individualAccountCurrBalance = new BigDecimal(mainExt.element("IndividualAccountCurrBalance").getText());
            } else {
                this.individualAccountCurrBalance = new BigDecimal(-0.0000999);
            }
            if (mainExt.element("IndividualAccountPrevPayment") != null) {
                this.individualAccountPrevPayment = new BigDecimal(mainExt.element("IndividualAccountPrevPayment").getText());
            } else {
                this.individualAccountPrevPayment = new BigDecimal(-0.0000999);
            }
            if (mainExt.element("IndividualAccountPrevBalance") != null) {
                this.individualAccountPrevBalance = new BigDecimal(mainExt.element("IndividualAccountPrevBalance").getText());
            } else {
                this.individualAccountPrevBalance = new BigDecimal(-0.0000999);
            }
            if (mainExt.element("OrgType") != null) {
                this.orgType = mainExt.element("OrgType").getText();
            }
        }
    
        Element payerParty = elementMain.element("PayerParty");
        if (payerParty != null) {
            if (payerParty.element("PayerPartyCode") != null) {
                this.payerPartyCode = payerParty.element("PayerPartyCode").getText();
            }
            if (payerParty.element("PayerPartyName") != null) {
                this.payerPartyName = payerParty.element("PayerPartyName").getText();
            }
        }
    
        this.detailsList = new ArrayList<>();
    
        Element auxDetails = elm.element("EInvoiceData").element("AuxDetails");
        if (auxDetails != null) {
            List nodes = auxDetails.elements("AuxItem");
            for (Iterator it = nodes.iterator(); it.hasNext(); ) {
                Element element = (Element) it.next();
                detailsList.add(new Details(element));
            }
        }
    
        //竖版票据解析
        if (elm.element("Header").element("EInvoiceID") != null) {
            this.eInvoiceId = elm.element("Header").element("EInvoiceID").getText();
        }
        Element outMedicalExtInfo = elementMain.element("MainExt").element("OutMedicalExtInfo");
        if (outMedicalExtInfo != null) {
            if (outMedicalExtInfo.element("HealthCareAccount") != null) {
                String HealthCareAccount = outMedicalExtInfo.element("HealthCareAccount").getText();
                Pattern r = compile("((基金支付)|(统筹支付)):[0-9|\\.]+");
                Matcher m = r.matcher(HealthCareAccount);
                if (m.find()) {
                    this.fundPayAmount = new BigDecimal(m.group(0).replaceAll("[^(0-9)|\\.]", ""));
                } else {
                    this.fundPayAmount = new BigDecimal(-0.0000999);
                }
            }
        
        }
        if (elm.element("EInvoiceData").element("Details") != null) {
            auxDetails = elm.element("EInvoiceData").element("Details");
            detailList = new ArrayList<>();
            if (auxDetails != null) {
                List nodes = auxDetails.elements("Item");
                for (Iterator it = nodes.iterator(); it.hasNext(); ) {
                    Element element = (Element) it.next();
                    detailList.add(new Detail(element));
                }
            }
            if (elementMain.element("MainExt").element("MedicalEInvoice") != null) {
                this.orgType = elementMain.element("MainExt").element("MedicalEInvoice").element("MedicalType").getText();
            }
        }
        //自费总金额
        BigDecimal selfpaymentCostSum = BigDecimal.ZERO;
        //自理总金额
        BigDecimal selfpaymentCareSum = BigDecimal.ZERO;
      BigDecimal itemAmountSum = BigDecimal.ZERO;
        for (Detail detail : detailList) {
          if (detail.getItemAmountPercent() != null) {
            if (detail.getItemAmountPercent() == 100) {
              selfpaymentCostSum = selfpaymentCostSum.add(detail.getItemAmountSelf());
            } else {
              selfpaymentCareSum = selfpaymentCareSum.add(detail.getItemAmountSelf());
            }
          }
          if (detail.getItemAmount() != null) {
            itemAmountSum = itemAmountSum.add(detail.getItemAmount());
          }
        }
      this.selfpaymentCost = selfpaymentCost.add(selfpaymentCostSum);
      this.selfpaymentCare = selfpaymentCare.add(selfpaymentCareSum);
  
      if (mainExt.element("MedicalEInvoice") != null) {
        if (mainExt.element("MedicalEInvoice").element("IdCardNo") != null) {
      
          this.payerPartyCode = mainExt.element("MedicalEInvoice").element("IdCardNo").getText();
        }
        if (mainExt.element("MedicalEInvoice").element("PatientMedicalInsuranceType") != null) {
          this.patientMedicalInsuranceType = mainExt.element("MedicalEInvoice").element("PatientMedicalInsuranceType").getText();
        }
      }
  
      if (mainExt.element("OutMedicalExtInfo") != null) {
        if (outMedicalExtInfo.element("IndividualAccountCurrPayment") != null) {
          this.individualAccountCurrPayment = new BigDecimal(outMedicalExtInfo.element("IndividualAccountCurrPayment").getText());
        } else {
      
          this.individualAccountCurrPayment = new BigDecimal(-0.0000999);
        }
        if (outMedicalExtInfo.element("IndividualAccountCurrBalance") != null) {
              this.individualAccountCurrBalance = new BigDecimal(outMedicalExtInfo.element("IndividualAccountCurrBalance").getText());
          } else {
        
              this.individualAccountCurrBalance = new BigDecimal(-0.0000999);
          }
          if (outMedicalExtInfo.element("IndividualAccountPrevPayment") != null) {
              this.individualAccountPrevPayment = new BigDecimal(outMedicalExtInfo.element("IndividualAccountPrevPayment").getText());
          } else {
        
              this.individualAccountPrevPayment = new BigDecimal(-0.0000999);
          }
          if (outMedicalExtInfo.element("IndividualAccountPrevBalance") != null) {
              this.individualAccountPrevBalance = new BigDecimal(outMedicalExtInfo.element("IndividualAccountPrevBalance").getText());
          } else {
              this.individualAccountPrevBalance = new BigDecimal(-0.0000999);
          }
      }
    }
}
